import './index.css'
import 'bootstrap/dist/css/bootstrap.min.css'
//处理图片资源
// document.querySelector('body') 
import { checkPhone, checkCode } from '@/utils/check.js'
import axios from '@/utils/request.js'
import { MyAlert } from '@/utils/myAlert.js'
// 不用浏览器脚本script导入，那么就得用Node去导入
const btn = document.querySelector('.btn')
btn.onclick = async () => {
  const mobile = document.querySelector('[name=mobile]').value
  const code = document.querySelector('[name=code]').value
  if (!checkPhone(mobile)) {
    MyAlert(false, '手机号长度必须为11位')
    return
  }

  if (!checkCode(code)) {
    MyAlert(false, '验证码长度必须为6位')
    return
  }
  try {
    const res = await axios({
      url: '/v1_0/authorizations',
      method: 'POST',
      data: {
        mobile,
        code
      }
    })
    //设置token
    localStorage.setItem('token', res.data.token)

    MyAlert(true, '登录成功')
    setTimeout(() => {
      location.href = '../content/content.html'
    }, 1500)
  } catch (error) {
    MyAlert(false, '登录失败')
  }
}
